package com.aliyun.messagemgt.service;

import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.common.PagerEntity;
import com.alibaba.algo.cooperation.feign.service.OrgFeignService;
import com.alibaba.algo.cooperation.feign.service.PositionFeignService;
import com.alibaba.algo.cooperation.feign.service.RoleFeignService;
import com.alibaba.algo.cooperation.feign.service.UserFeignService;
import com.alibaba.algo.cooperation.feign.vo.req.*;
import com.alibaba.algo.cooperation.feign.vo.res.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.messagemgt.common.dto.foura.*;
import com.aliyun.messagemgt.common.dto.response.ResultMap;
import com.aliyun.messagemgt.convert.FouraConverter;
import com.aliyun.messagemgt.fouraplugin.FouraService;
import com.aliyun.messagemgt.util.ConverterUtil;
import com.aliyun.messagemgt.util.PageUtils;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@ConditionalOnProperty(prefix = "xxl.sso", name = "enable", havingValue = "true")
public class FouraServiceImpl implements FouraService {
    @Autowired
    private UserFeignService userFeignService;

    @Autowired
    private OrgFeignService orgFeignService;

    @Autowired
    private RoleFeignService roleFeignService;
    @Autowired
    private PositionFeignService positionFeignService;

    @Override
    public Map<String, Object> getUser() {
        return null;
    }

    @Override
    public List<DepartmentVo> getAllDepartment() {
        return null;
    }

    @Override
    public ResultMap getOrganizationInfoByParentCode(String orgCode) {
        return null;
    }

    @Override
    public FourAMenu getAuthByUser() {
        //String jsonText = "{\"menu\":[{\"key\":\"1-tspz\",\"name\":\"推送配置\",\"parent\":\"root\",\"url\":\"~\",\"order\":0,\"children\":[{\"key\":\"1qdgl-2\",\"name\":\"渠道管理\",\"parent\":\"1-tspz\",\"url\":\"/\",\"order\":0,\"children\":[{\"key\":\"1qdlb-3\",\"name\":\"渠道列表\",\"parent\":\"1qdgl-2\",\"url\":\"messageCenter/channelManager/channelList\",\"order\":0,\"children\":null}]},{\"key\":\"2mbgl-2\",\"name\":\"模板管理\",\"parent\":\"1-tspz\",\"url\":\"~\",\"order\":0,\"children\":[{\"key\":\"2mblb-3\",\"name\":\"模板列表\",\"parent\":\"2mbgl-2\",\"url\":\"messageCenter/templateManager\",\"order\":0,\"children\":null},{\"key\":\"1dxjmgl-3\",\"name\":\"短信签名列表\",\"parent\":\"2mbgl-2\",\"url\":\"messageCenter/templateManager/smsManager\",\"order\":0,\"children\":null}]},{\"key\":\"3xxygl-2\",\"name\":\"消息源管理\",\"parent\":\"1-tspz\",\"url\":\"~\",\"order\":0,\"children\":[{\"key\":\"1xxylb-3\",\"name\":\"消息源列表\",\"parent\":\"3xxygl-2\",\"url\":\"messageCenter/messageManager/messageSource\",\"order\":0,\"children\":null},{\"key\":\"2xxff-3\",\"name\":\"消息发送监控\",\"parent\":\"3xxygl-2\",\"url\":\"messageCenter/messageManager/messageSend\",\"order\":0,\"children\":null}]},{\"key\":\"4jszgl-2\",\"name\":\"接收组管理\",\"parent\":\"1-tspz\",\"url\":\"~\",\"order\":0,\"children\":[{\"key\":\"1jszlb-3\",\"name\":\"接收组列表\",\"parent\":\"4jszgl-2\",\"url\":\"messageCenter/AcceptGroup/list\",\"order\":0,\"children\":null}]}]},{\"key\":\"2-grzx\",\"name\":\"个人中心\",\"parent\":\"root\",\"url\":\"~ \",\"order\":0,\"children\":[{\"key\":\"1wdxx-2\",\"name\":\"我的消息\",\"parent\":\"2-grzx\",\"url\":\"messageCenter/messageManager/myMessage\",\"order\":0,\"children\":null},{\"key\":\"2grxxgl-2\",\"name\":\"个人信息管理\",\"parent\":\"2-grzx\",\"url\":\"messageCenter/personalInfoMgt/userManagement\",\"order\":0,\"children\":null}]},{\"key\":\"3-xtgl\",\"name\":\"系统管理\",\"parent\":\"root\",\"url\":\"~\",\"order\":0,\"children\":[{\"key\":\"1rzgl-2\",\"name\":\"日志管理\",\"parent\":\"3-xtgl\",\"url\":\"~\",\"order\":0,\"children\":[{\"key\":\"1czrz-3\",\"name\":\"操作日志\",\"parent\":\"1rzgl-2\",\"url\":\"systemManager/systemMessage/operateLog\",\"order\":0,\"children\":null},{\"key\":\"2jkrz-3\",\"name\":\"接口日志\",\"parent\":\"1rzgl-2\",\"url\":\"systemManager/systemManage/interfaceLog\",\"order\":0,\"children\":null}]}]}]}";
        String jsonText = "{\"menu\":[{\"key\":\"1-tspz\",\"name\":\"推送配置\",\"parent\":\"root\",\"url\":\"~\",\"order\":0,\"children\":[{\"key\":\"1qdgl-2\",\"name\":\"消息渠道管理\",\"parent\":\"1-tspz\",\"url\":\"messageCenter/channelManager/channelList\",\"order\":1,\"children\":null},{\"key\":\"2mbgl-2\",\"name\":\"消息模板管理\",\"parent\":\"1-tspz\",\"url\":\"~\",\"order\":2,\"children\":[{\"key\":\"2mblb-3\",\"name\":\"消息模板列表\",\"parent\":\"2mbgl-2\",\"url\":\"messageCenter/templateManager\",\"order\":1,\"children\":null},{\"key\":\"1dxjmgl-3\",\"name\":\"短信签名列表\",\"parent\":\"2mbgl-2\",\"url\":\"messageCenter/templateManager/smsManager\",\"order\":2,\"children\":null}]},{\"key\":\"3jszgl-2\",\"name\":\"接收组管理\",\"parent\":\"1-tspz\",\"url\":\"messageCenter/AcceptGroup/list\",\"order\":3,\"children\":null},{\"key\":\"4fsrwpz-2\",\"name\":\"发送任务配置\",\"parent\":\"1-tspz\",\"url\":\"messageCenter/messageManager/messageSource\",\"order\":4,\"children\":null},{\"key\":\"5xxfsjk-2\",\"name\":\"消息发送监控\",\"parent\":\"1-tspz\",\"url\":\"messageCenter/messageManager/messageSend\",\"order\":5,\"children\":null},{\"key\":\"6xxygl-2\",\"name\":\"消息源管理\",\"parent\":\"1-tspz\",\"url\":\"messageCenter/messageManager/msgSourceManage\",\"order\":6,\"children\":null}]},{\"key\":\"2-grzx\",\"name\":\"个人中心\",\"parent\":\"root\",\"url\":\"~\",\"order\":0,\"children\":[{\"key\":\"1wdxx-2\",\"name\":\"我的消息\",\"parent\":\"2-grzx\",\"url\":\"messageCenter/messageManager/myMessage\",\"order\":0,\"children\":null},{\"key\":\"2grxxgl-2\",\"name\":\"个人信息管理\",\"parent\":\"2-grzx\",\"url\":\"messageCenter/personalInfoMgt/userManagement\",\"order\":0,\"children\":null}]},{\"key\":\"3-xtgl\",\"name\":\"系统管理\",\"parent\":\"root\",\"url\":\"~\",\"order\":0,\"children\":[{\"key\":\"1zhxx-2\",\"name\":\"租户信息\",\"parent\":\"3-xtgl\",\"url\":\"messageCenter/messageManager/tenantManage\",\"order\":0},{\"key\":\"2rzgl-2\",\"name\":\"日志管理\",\"parent\":\"3-xtgl\",\"url\":\"~\",\"order\":0,\"children\":[{\"key\":\"1czrz-3\",\"name\":\"操作日志\",\"parent\":\"1rzgl-2\",\"url\":\"systemManager/systemMessage/operateLog\",\"order\":0,\"children\":null},{\"key\":\"2jkrz-3\",\"name\":\"接口日志\",\"parent\":\"1rzgl-2\",\"url\":\"systemManager/systemManage/interfaceLog\",\"order\":0,\"children\":null}]}]}]}";
        FourAMenu fourAMenu = JSONObject.parseObject(jsonText, FourAMenu.class);
        return fourAMenu;
    }


    @Override
    public String getSsoUser(HttpServletRequest request) {
        return null;
    }

    @Override
    public List<OrgInfo> getRootTree() {
        List<FeignGetFirstLevelOrgResponseVO> firstLevelOrgList = orgFeignService.getFirstLevelOrgList();
        List<OrgInfo> result = new ArrayList<>();
        for (FeignGetFirstLevelOrgResponseVO vo : firstLevelOrgList) {
            OrgInfo orgInfo = new OrgInfo();
            orgInfo.setOrgId(vo.getId().toString());
            orgInfo.setOrgName(vo.getOrgName());
            orgInfo.setOrgType(vo.getOrgType().toString());
            orgInfo.setParentId(vo.getParentId().toString());
            result.add(orgInfo);
        }
        return result;
    }

    @Override
    public PageInfo<UserInfo> getUserByOrgCode(String orgCode, String userName, Integer pageSize, Integer pageNo) {
        return null;
    }

    @Override
    public List<Map<String, Object>> getAllUserInfo(Integer pageSize, Integer pageNo) {
        return null;
    }

    @Override
    public int sync4AData() {
        return 0;
    }

    @Override
    public List<FouraOrgUserInfoDTO> selectUserByOrg(FouraGetUserByOrgQry qry) {
        SelectOrgUserReqVO selectOrgUserReqVO = FouraConverter.INSTANCE.fouraGetUserByOrgQry2selectOrgUserReqVO(qry);
        // 用户组件接口中orgId改为orgIdList，此字段废弃
        selectOrgUserReqVO.setOrgId(null);
        List<OrgUserInfoResVO> orgUserInfoResVOList = orgFeignService.qryUserListByOrg(selectOrgUserReqVO).getData();
        return FouraConverter.INSTANCE.orgUserInfoResVO2fouraOrgUserInfoDTOList(orgUserInfoResVOList);
    }

    @Override
    public List<FouraGetFirstLevelOrgDTO> getFirstLevelOrgList() {
        List<FeignGetFirstLevelOrgResponseVO> feignGetFirstLevelOrgResponseVOList = orgFeignService.getFirstLevelOrgList();
        return FouraConverter.INSTANCE.feignGetFirstLevelOrgResponseVOList2fouraGetFirstLevelOrgDTOList(feignGetFirstLevelOrgResponseVOList);
    }

    @Override
    public List<FouraOrgInfoDTO> getOrgListByParentOrgId(FouraGetOrgListByParentIdQry qry) {
        ConverterUtil converter = ConverterUtil.getInstance();
        SelectParentOrgReqVO vo = converter.map(qry, SelectParentOrgReqVO.class);
        BaseResult<List<OrgInfoResVO>> baseRes = orgFeignService.qryOrgListByParentOrgId(vo);
        List<OrgInfoResVO> vos = baseRes.getData();
        return converter.mapAsList(vos, FouraOrgInfoDTO.class);
    }

    @Override
    public PageInfo<FouraRoleDTO> selectRoleByPage(FouraPageRoleQry pageQry) {
        FeignSelectRoleListRequestVO vo = FouraConverter.INSTANCE.fouraPageRoleQry2feignSelectRoleListRequestVO(pageQry);
        log.info("selectRoleByPage pageQry= {}", JSON.toJSONString(pageQry));
        log.info("selectRoleByPage vo= {}", JSON.toJSONString(vo));
        BaseResult<PagerEntity<FeignRoleListResponseVO>> result = roleFeignService.selectPage(vo);
        PageInfo<FeignRoleListResponseVO> feignRoleListResponseVOPageInfo = PageUtils.of(result.getData());
        PageInfo<FouraRoleDTO> fouraRoleDTOPageInfo = FouraConverter.INSTANCE.feignRoleListResponseVOPageInfo2fouraRoleDTOPageInfo(feignRoleListResponseVOPageInfo);
        log.info("selectRoleByPage result= {}", JSON.toJSONString(result));
        return fouraRoleDTOPageInfo;
    }

    /**
     * 根据组织id获取岗位信息
     *
     * @param orgId
     * @return
     */
    @Override
    public List<FouraPositionDTO> selectPositionByOrg(Long orgId) {
        BaseResult<List<SysPositionVO>> baseResult = positionFeignService.list(orgId);
        List<FouraPositionDTO> res = new ArrayList<>();
        if (ObjectUtils.isEmpty(baseResult) || ObjectUtils.isEmpty(baseResult.getData())) {
            return res;
        }
        res = ConverterUtil.getInstance().mapAsList(baseResult.getData(), FouraPositionDTO.class);
        return res;
    }

    @Override
    public List<FouraPositionUserInfoDTO> qryUserListByPositionId(FouraPositionUserQry qry) {
        SelectPositionUserReqVO vo = FouraConverter.INSTANCE.fouraPositionUserQry2selectPositionUserReqVO(qry);
        BaseResult<List<PositionUserInfoResVO>> PositionUserInfoResVOSResult = positionFeignService.qryUserListByPositionId(vo);
        List<PositionUserInfoResVO> positionUserInfoResVOList = PositionUserInfoResVOSResult.getData();
        return FouraConverter.INSTANCE.positionUserInfoResVOList2fouraPositionUserInfoDTO(positionUserInfoResVOList);
    }

    @Override
    public List<FouraRoleUserInfoDTO> selectUserListByRoleIdListOrAppCodeList(FouraRoleUserQry qry) {
        SelectUserListByRoleIdListReqVO vo = FouraConverter.INSTANCE.fouraRoleUserQry2selectUserListByRoleIdListReqVO(qry);
        BaseResult<List<RpcUserInfoVO>> rpcUserInfoVOSResult = roleFeignService.selectUserListByRoleIdListOrAppCodeList(vo);
        List<RpcUserInfoVO> rpcUserInfoVOList = rpcUserInfoVOSResult.getData();
        return FouraConverter.INSTANCE.rpcUserInfoVOList2fouraRoleUserInfoDTOList(rpcUserInfoVOList);
    }

    @Override
    public List<FouraUserInfoDTO> qryUserInfoByUserListOrAppCode(FouraUserQry qry) {
        SelectUserListInfoReqVo vo = FouraConverter.INSTANCE.fouraUserQry2selectUserListInfoReqVo(qry);
        BaseResult<List<RpcUserInfoVO>> rpcUserInfoVOSResult = userFeignService.qryUserInfoByUserListOrAppCode(vo);
        List<RpcUserInfoVO> rpcUserInfoVOList = rpcUserInfoVOSResult.getData();
        return FouraConverter.INSTANCE.rpcUserInfoVOList2fouraUserInfoDTOList(rpcUserInfoVOList);
    }

    @Override
    public FouraUserInfoDTO checkLogin(FouraUserLoginQry fouraUserLoginQry) {
        SelectUserLoginReqVO vo =  new SelectUserLoginReqVO();
        FouraUserInfoDTO dto = new FouraOrgUserInfoDTO();
        BeanUtils.copyProperties(fouraUserLoginQry,vo);
        BaseResult<LoginUserInfoResVO> baseResult = userFeignService.checkLogin(vo);
        LoginUserInfoResVO loginUserInfoResVO = baseResult.getData();
        BeanUtils.copyProperties(loginUserInfoResVO,dto);
        return dto;
    }
}
